package com.java2104.controller;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
public class StaffController {
    @InitBinder//会在处理方法执行前，将参数类型进行判断和转化
    public  void initBinder(WebDataBinder webDataBinder,String height){
        //将客户端发送的请求参数中的“yyyy-MM-dd” 的日期转换成java.util.Date的对象
        CustomDateEditor customDateEditor
                =new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),false);
        webDataBinder.registerCustomEditor(java.util.Date.class,customDateEditor);
        webDataBinder.registerCustomEditor(String.class,new StringTrimmerEditor(false));
        if (height==null||height.equals("")){
            webDataBinder.setDisallowedFields("height");
        }
    }
    @GetMapping("/addStaff")
    public  String addStaff(){
        return "addStaff";
    }
    @PostMapping("/addStaff")
    public String addStaff(String name, String sex, Date birthday, Float height, Model model){
       model.addAttribute("name",name);
       model.addAttribute("sex",sex);
       model.addAttribute("birthday",birthday);
       model.addAttribute("height",height);
       model.addAttribute("message","添加员工成功");
        return "addStaff";
    }
}
